<script lang="ts" setup>
import { ref } from 'vue'
import { Plus } from '@element-plus/icons-vue'
import { QuillEditor } from '@vueup/vue-quill'
import '@vueup/vue-quill/dist/vue-quill.snow.css'
// 表单数据
const defaultForm = {
  title: '',
  cate_id: '',
  cover_img: '',
  content: '',
  state: '',
  id: ''
}
const formModel = ref({ ...defaultForm })
const rules = {
  title: [
    {
      required: true,
      message: '请输入标题',
      trigger: 'blur'
    }
  ],
  cate_id: [
    {
      required: true,
      message: '请选择分类',
      trigger: 'change'
    }
  ],
  cover_img: [
    {
      required: true,
      message: '请选择分类',
      trigger: 'change'
    }
  ]
}
// 抽屉
const drawer = ref(false)
const imgUrl = ref('')
const onUploadFile = (UploadFile) => {
  imgUrl.value = URL.createObjectURL(UploadFile.raw)
  formModel.value.cover_img = UploadFile.raw
}

const open = () => {
  drawer.value = true
}
defineExpose({
  open
})
</script>

<template>
  <div class="common-layout">
    <el-drawer
      size="50%"
      v-model="drawer"
      :title="formModel.id ? '编辑文章' : '发布文章'"
      direction="rtl"
    >
      <el-form :model="formModel" :rules="rules">
        <el-form-item prop="title" label="文章标题">
          <el-input
            v-model="formModel.title"
            placeholder="请输入文章标题"
          ></el-input>
        </el-form-item>
        <el-form-item prop="cate_id" label="文章分类">
          <el-select v-model="formModel.cate_id" placeholder="请选择分类">
            <el-option :label="'前端'" :key="1" :value="1"></el-option>
            <el-option :label="'后端'" :key="2" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="cover_img" label="上传图片">
          <el-upload
            class="avatar-uploader"
            :show-file-list="false"
            :auto-upload="false"
            :on-change="onUploadFile"
          >
            <img v-if="imgUrl" :src="imgUrl" class="avatar" />
            <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
          </el-upload>
        </el-form-item>
        <el-form-item label="文章内容">
          <div class="editor">
            <quill-editor
              theme="snow"
              v-model:content="formModel.content"
              contentType="html"
            >
            </quill-editor>
          </div>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<style lang="scss" scoped>
.avatar-uploader {
  :deep() {
    .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }
    .el-upload {
      border: 1px dashed var(--el-border-color);
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      transition: var(--el-transition-duration-fast);
    }
    .el-upload:hover {
      border-color: var(--el-color-primary);
    }
    .el-icon.avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      text-align: center;
    }
  }
}
.editor {
  width: 100%;
  :deep(.ql-editor) {
    min-height: 200px;
  }
}
</style>
